package uit.minito.views;

import uit.minito.model.MyPoint;
import uit.minito.suggestion.SuggestionEditText;
import uit.minito.suggestion.SuggestionModel;
import uit.minito.timduongxebuyt.R;
import uit.minito.timduongxebuyt.StaticVariables;
import android.app.AlertDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;





public class InputLayout extends LinearLayout implements OnClickListener{

	Button searchButton;

	public SuggestionEditText locationField;
	public SuggestionEditText destinationField;
	ImageView inputPuller;
	LinearLayout mainLayout;
	OnInputAddressListener onInputAddressListener;
	boolean isOnTop;
	
	public static enum FieldKind{
		Location,
		Destination
	};

	private void initialize(Context context) {
		// inflat Layout from XML file
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.input, this);
		
		// get all the controls and store in our variables
		getControls();
		// Do Stuff with Controls
		setUpControls(context);
		isOnTop=false;
	}
	
	public void fillPointToTextField(MyPoint _point,FieldKind _index){
		String addressLine=String.valueOf(_point.getLat())+", "+String.valueOf(_point.getLng());
		SuggestionModel suggestion=new SuggestionModel(addressLine, _point.getLat(), _point.getLng());
		switch (_index) {
		case Location:
			locationField.fillWithModel(suggestion);
			break;

		case Destination:
			destinationField.fillWithModel(suggestion);
			break;
		}
	}
	
	public void setOnInputAddressListener(OnInputAddressListener _delegate){
		this.onInputAddressListener=_delegate;
	}
	public InputLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	public InputLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		initialize(context);
	}

	// get Control from XML
	private void getControls() {
		searchButton = (Button) findViewById(R.id.searchButton);

		locationField = (SuggestionEditText) findViewById(R.id.locationField);
		destinationField = (SuggestionEditText) findViewById(R.id.destinationField);
		inputPuller = (ImageView) findViewById(R.id.inputPuller);
		mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
	}
	
	private void doSlideAnimation(float _fromY,final float _toY){
		TranslateAnimation translateAnimation=new TranslateAnimation(0, 0, _fromY,MyUtilities.convertToDP(_toY, getContext()));
		translateAnimation.setDuration(StaticVariables.SLIDE_DURATION);
		translateAnimation.setAnimationListener(new Animation.AnimationListener() {
			
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				
				LinearLayout.LayoutParams mainParam=(LinearLayout.LayoutParams)mainLayout.getLayoutParams();
				if(isOnTop){
					mainParam.topMargin=0;  
				}else{
					mainParam.topMargin=MyUtilities.convertToDP(_toY, getContext());
				}
				
				mainLayout.setLayoutParams(mainParam);
				isOnTop=!isOnTop;
				mainLayout.clearAnimation();
			}
		});
		mainLayout.startAnimation(translateAnimation);
	}
	
	private void doRotateAnimation(float _fromDegrees, float _toDegrees,final int _res ){

		RotateAnimation anim=new RotateAnimation(_fromDegrees, _toDegrees, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		anim.setDuration(StaticVariables.SLIDE_DURATION);
		anim.setFillAfter(true);
		anim.setAnimationListener(new AnimationListener() {
			
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				//inputPuller.clearAnimation();
				
					
				//inputPuller.clearAnimation();
					//inputPuller.setBackgroundResource(_res);
					

				
			}
		});
		inputPuller.startAnimation(anim);
	}
	private void showAlert(int _msg){
		new AlertDialog.Builder(getContext()).setTitle(R.string.alert_title).setMessage(_msg).setNeutralButton(R.string.alert_close_text, null).show();
	}
	public void clearAllField(){
		locationField.reset();
		destinationField.reset();
	}
	public boolean keyBoardIsOnScreen(){
		return locationField.isFocusable()||destinationField.isFocusable();
	}
	public SuggestionEditText getCurrentFocusEditText(){
		if(locationField.isFocused()){
			return locationField;
		}
		if(destinationField.isFocused()){
			return destinationField;
		}
		return null;
	}
	public void slideUp(){
		if(isOnTop==false){
			doSlideAnimation(0, -StaticVariables.INPUT_CONTENT_HEIGHT);
			doRotateAnimation(0, 180, R.drawable.input_puller_down);
			
		}
	}
	public void slideDown(){
		if(isOnTop==true){
			doSlideAnimation(0, StaticVariables.INPUT_CONTENT_HEIGHT);
			doRotateAnimation(180, 360,R.drawable.input_puller_up);
			
		} 
	}
	private void setUpControls(Context context) {
		// locationField.initialize(context);
		// destinationField.initialize(context);
  

		inputPuller.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:  
					
					if(isOnTop==false){
						doSlideAnimation(0, -StaticVariables.INPUT_CONTENT_HEIGHT);
						doRotateAnimation(0, 180, R.drawable.input_puller_down);
						onInputAddressListener.onPressSlideUp();
					}else{
						doSlideAnimation(0, StaticVariables.INPUT_CONTENT_HEIGHT);
						doRotateAnimation(180, 360,R.drawable.input_puller_up);
						onInputAddressListener.onPressSlideDown();
					}  
					
					break;
				case MotionEvent.ACTION_UP:
					break;

				}
				return true;
			}
		});
		
		searchButton.setOnClickListener(this);

	}
	public void beginSearching(){
		if(locationField.isUserHasChosenASuggestion()==false){
			showAlert(R.string.alert_no_location);
			return;
		}
		if(destinationField.isUserHasChosenASuggestion()==false){
			showAlert(R.string.alert_no_destination);
			return;
		}
		double startLat=locationField.getCurrentSuggestion().getLatitude();
		double startLong=locationField.getCurrentSuggestion().getLongitude();
		double endLat=destinationField.getCurrentSuggestion().getLatitude();
		double endLong=destinationField.getCurrentSuggestion().getLongitude();
		onInputAddressListener.search(startLat, startLong, endLat, endLong);
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.searchButton:
			beginSearching();
			break;

//		case R.id.swapButton:
//			SuggestionModel tempSuggestion=locationField.getCurrentSuggestion();
//			locationField.fillWithModel(destinationField.getCurrentSuggestion());
//			destinationField.fillWithModel(tempSuggestion);
//			onInputAddressListener.onPressSwap();
//			break;
		}
	}
}
